<template>
  <div>
    <div ref="basicCanvas" style="height: 100%; width: 100%"></div>
  </div>
</template>

<script>
import { resize } from "./resize"
export default {
  name: "BarChart",
  mixins: [ resize ],
  data() {
    return {
      myChart: null,
    };
  },
  props: {
    chartData: {
      type: Array,
      default: () => [[], []],
    },
  },
  watch: {
    chartData: {
      handler(val){
        this.initChart(val);
      },
      deep: true,
    },
  },
  created(){
    this.initChart(this.chartData);
  },
  methods: {
    async initChart(val) {
      await this.$nextTick()
      if(this.myChart) { this.myChart.dispose() };
      this.myChart = this.$echarts.init(this.$refs.basicCanvas);
      let max = Math.max(...val[1]),
          min = Math.min(...val[1]);
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        legend: {
          data: ["存存量客户收入保有率(%)"],
        },
        grid: {
          left: "30px",
          right: "30px",
          // top: "60px",
          bottom: "80px",
        },
        xAxis: {
          type: "category",
          data: val[0],
          axisLabel: {
            interval: 0,
            // rotate: 33,
            formatter(val){
              return val.split("").join("\n")
            }
          },
          textStyle: {
            fontSize: "8px",
          }
        },
        yAxis: {
          type: "value",
          max: max,
          min: min / 1.05,
          splitLine: { show: false }, //去除网格线
          axisLabel: {
            formatter: "{value}",
            show: false,
          },
          axisTick: {
            // y轴刻度线
            show: false,
          },
          axisLine: {
            // y轴
            show: false,
          },
        },

        series: [
          {
            type: "bar",
            name: "存存量客户收入保有率(%)",
            data: val[1],
            yAxisIndex: 0,
            barWidth: 22,
            color: "rgb(222,235,247)",
            borderWidth: 1,
            itemStyle: {
              normal: {
                color(params){
                  if (params.name === '浙江' || params.name === '全省') return 'rgb(255,255,204)';
                  return 'rgb(183,222,232)'
                },
                borderColor: 'rgb(211,211,199)',
                borderWidth: 1,
                label: {
                  show: true,
                  position: "top",
                  textStyle: {
                    color: "#000",
                    fontSize: '8px',
                  },
                  formatter: "{c}",
                },
              },
            },
          },
        ],
      };
      this.myChart.setOption(option);
    },
  },
};
</script>

<style>
</style>
